package com.bydq.patrolsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bydq.patrolsystem.entity.PsRole;
import com.bydq.patrolsystem.mapper.PsRoleMapper;
import com.bydq.patrolsystem.service.PsRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 写代码是不可能写代码的啦~~
 * @since 2020-05-22
 */
@Service
public class PsRoleServiceImpl extends ServiceImpl<PsRoleMapper, PsRole> implements PsRoleService {

    @Autowired
    private PsRoleMapper psRoleMapper;

    /**
     *
     * @param currentPage 当前页
     * @param pageSize  每页显示数量
     * @param roleName  角色名字
     * @param isStart   是否启用
     * @return
     */
    @Override
    public IPage<PsRole> selectPageByEnditions(Integer currentPage, int pageSize, String roleName, String isStart) {
        Page<PsRole> psRolePage = new Page<>(currentPage, pageSize);



        //如果roleName不为null 同时 isState不为null 同时 isState还不是""
        QueryWrapper<PsRole> wrapper = new QueryWrapper<PsRole>().
                like(roleName != null, "roleName", roleName).
                eq(isStart != null && !"".equals(isStart.trim()), "isStart", isStart);

        //返回分页对象
        return psRoleMapper.selectPage(psRolePage,wrapper);
    }

    @Override
    public boolean setStart(Integer id, Integer start) {
       return psRoleMapper.updateIsStartById(id,start)>0;
    }

    @Override
    public List<PsRole> listStartRole() {
        //查询启用的角色
        return psRoleMapper.selectList(new QueryWrapper<PsRole>().eq("isStart",1));
    }
}
